/**
 * @file skoll.actions.rule.dor.js
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: skoll.actions.rule.dor.js 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/skins/Skoll/js/skoll.actions.rule.dor.js $
 */

Skoll.actions.rule.dor = {
	//
	// Protected properties.
	//
	// Public methods.
	create: function(form, ruleId) {
		Skoll.tools.Loading.start(0);
		Skoll.tools.preventDefault();

		var nameInput = $(form).find("#action_name");
		var modeInput = $(form).find("#action_mode");
		var name = nameInput.val().trim();
		var mode = modeInput.val().trim();

		if (name.length > 0 && mode.length > 0) {
			$.ajax({
				type: "POST",
				url: "?action=rule:dor:create&group=" + GROUP_ID + "&rule=" + ruleId + "&format=json" + Skoll.tools.extraUrlParams(),
				data: {
					name: name,
					mode: mode
				},
				dataType: "json",
				success: function(data) {
					console.log(data);
					if (data.status) {
						window.location.href = "?action=rule:dor:edit&group=" + GROUP_ID + "&rule=" + ruleId + "&raction=" + data.info_id + Skoll.tools.extraUrlParams();
					} else {
						alert(wcomix.lang.translate("message_unable_create_rule_action"));
						Skoll.tools.Loading.stop();
					}
				},
				error: function(data) {
					console.log(data);
					if (typeof data.responseText != "undefined") {
						console.log(data.responseText);
					}

					alert(wcomix.lang.translate("message_unknown_error"));
					Skoll.tools.Loading.stop();
				}
			});
		} else {
			if (name.length <= 0) {
				nameInput.focus();
				alert(wcomix.lang.translate("message_request_name"));
			} else if (mode.length <= 0) {
				modeInput.focus();
				alert(wcomix.lang.translate("message_request_mode"));
			}

			Skoll.tools.Loading.stop();
		}
	},
	editCMOVE: function(form) {
		Skoll.actions.rule.dor._editGENDirCol("CMOVE", form);
	},
	editCOLLECTOR: function(form) {
		Skoll.actions.rule.dor._editGENDirCol("COLLECTOR", form);
	},
	editMOVE: function(form) {
		Skoll.actions.rule.dor._editGENDirTag("MOVE", form);
	},
	editTAGGER: function(form) {
		Skoll.actions.rule.dor._editGENDirTag("TAGGER", form);
	},
	editVMOVE: function(form) {
		Skoll.actions.rule.dor._editGENDirTag("VMOVE", form);
	},
	//
	// Protected methods.
	_editGENDirTag: function(mode, form) {
		Skoll.tools.preventDefault();
		Skoll.tools.Loading.start(0);

		var ruleId = $(form).find("#ruleid").val();
		var actionId = $(form).find("#actionid").val();
		var url = "?action=rule:dor:edit&group=" + GROUP_ID + "&rule=" + ruleId + "&raction=" + actionId + "&format=json&nolayout";

		var actionName = $(form).find("#name").val();
		var directory = $(form).find("#directory").val();
		var tags = $(form).find("#tags").val();
		var status = $(form).find("#status").val();

		var send = true;

		if (send) {
			$.ajax({
				dataType: "json",
				url: url,
				type: "POST", data: {
					name: actionName,
					mode: mode,
					directory: directory,
					tags: tags,
					status: status
				},
				success: function(data) {
					if (!data.status) {
						alert(data.status_message);
						Skoll.tools.Loading.stop();
					} else {
						Skoll.tools.reload();
					}
				},
				error: function(data) {
					Skoll.tools.log("Tags settings error: " + data);
					Skoll.tools.Loading.stop();
				}
			});
		} else {
			alert(wcomix.lang.translate("message_nothing_to_send"));
			Skoll.tools.Loading.stop();
		}
	},
	_editGENDirCol: function(mode, form) {
		Skoll.tools.preventDefault();
		Skoll.tools.Loading.start(0);

		var ruleId = $(form).find("#ruleid").val();
		var actionId = $(form).find("#actionid").val();
		var url = "?action=rule:dor:edit&group=" + GROUP_ID + "&rule=" + ruleId + "&raction=" + actionId + "&format=json&nolayout";

		var actionName = $(form).find("#name").val();
		var directory = $(form).find("#directory").val();
		var collection = $(form).find("#collection").val();
		var status = $(form).find("#status").val();

		var send = true;

		if (send) {
			$.ajax({
				dataType: "json",
				url: url,
				type: "POST", data: {
					name: actionName,
					mode: mode,
					directory: directory,
					collection: collection,
					status: status
				},
				success: function(data) {
					if (!data.status) {
						alert(data.status_message);
						Skoll.tools.Loading.stop();
					} else {
						Skoll.tools.reload();
					}
				},
				error: function(data) {
					Skoll.tools.log("Tags settings error: " + data);
					Skoll.tools.Loading.stop();
				}
			});
		} else {
			alert(wcomix.lang.translate("message_nothing_to_send"));
			Skoll.tools.Loading.stop();
		}
	}
};
